package tack11;

import chapter11.SuperArray;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 学生管理系统
 */
public class StudentUI {
    SuperArray ary = new SuperArray();
    Student student = new Student();
    Scanner sc = new Scanner(System.in);

    public StudentUI() {
        ary.add(new Student("张飞", "男", "20"));
        ary.add(new Student("关羽", "男", "20"));
        ary.add(new Student("刘备", "女", "22"));
        menu();
    }

    /**
     * 菜单界面
     */
    public void menu() {
        System.out.println("欢迎使用学生管理程序");
        while (true) {
            System.out.println("请选择：1-查看所有学生 2-增加 3-修改 4-删除 5-退出 6-查询");
            int input ;
            try {
                input = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入类型错误");
                sc.next();
                continue;
            }
            if (input == 1) {
                show();
            } else if (input == 2) {
                add();
            } else if (input == 3) {
                update();
            } else if (input == 4) {
                remove();
            } else if (input == 5) {
                over();
            } else if (input == 6) {
                inquire();
            } else {
                System.out.println("无此选项重新选择");
            }
        }
    }

    /*** 显示所有学生*/
    public void show() {
        for (int i = 0; i < ary.size(); i++) {
            Student student = (Student) ary.gat(i);
            System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
        }
    }

    /*** 增加学生*/
    public void add() {
        while (true) {
            System.out.println("请输入该学生的 姓名 性别 年龄");
            System.out.println("请输入姓名");
            String name = sc.next();
            if (name.matches("^[\\u4e00-\\u9fa5]{2,}$")) {
                System.out.println("请输入性别");
                String gender = sc.next();
                if (gender.matches("^[男女]{1}$")) {
                    System.out.println("请输入年龄");
                    String age = sc.next();
                    if (age.matches("^[1]{1}[8-9]{1}|[2]{1}[0-9]{1}|[3]{1}[0]{1}$")) {
                        ary.add(new Student(name, gender, age));
                        break;

                    }else {
                        System.out.println("输入有误重新输入");
                    }
                }
            }
        }
    }

    /*** 修改学生*/
    public void update() {
        while (true) {
            System.out.println("输入学生的名字");
            String name = sc.next();
            for (int i = 0; i < ary.size(); i++) {
                Student student = (Student) ary.gat(i);
                if (name.equals(student.getName())) {
                    System.out.println("请输入要修改信息 1 名字 2 性别 3 年龄");
                    int a ;
                    try {
                         a = sc.nextInt();
                    }catch (InputMismatchException e){
                        System.out.println("输入有误重新输入");
                        sc.next();
                        continue;
                    }
                    if (a == 1) {
                        System.out.println("请输入更正后的姓名");
                        String name1 = sc.next();
                        if (name1.matches("^[\\u4e00-\\u9fa5]{2,}$")) {
                            student.setName(name1);
                            System.out.println("更正成功");
                            return;
                        }else {
                            System.out.println("输入有误");
                            break;
                        }
                    } else if (a == 2) {
                        System.out.println("请输入更正性别");
                        String gender = sc.next();
                        if (gender.matches("^[男女]{1}$")) {
                            student.setGender(gender);
                            System.out.println("更正成功");
                            return;
                        }else {
                            System.out.println("输入性别有误");
                            continue;
                        }
                    }
                    while (true) {
                        if (a == 3) {
                            System.out.println("请输入更正后的年龄");
                            String age ;
                            try {
                                age = sc.next();
                            } catch (InputMismatchException e) {
                                System.out.println("输入年龄类型错误");
                                sc.next();
                                continue;
                            }
                            if (age.matches("^[1]{1}[8-9]{1}|[2]{1}[0-9]{1}|[3]{1}[0]{1}$")) {
                                System.out.println("更正成功");
                                student.setAge(age);
                                break;
                            }else {
                                System.out.println("年龄过大或过小重新输入");
                                break;
                            }
                        }else {
                            System.out.println("查无此人");
                            break;
                        }
                    }
                }
            }
        }

    }


    /*** 删除学生*/
    public void remove() {
        System.out.println("请输入要删除同学的名字");
        String name = sc.next();
        for (int i = 0; i < ary.size(); i++) {
            if (name.equals(student.getName())) {
                ary.remove(i);
                System.out.println("删除成功");
                return;
            }

        }
        System.out.println("没有这位同学");
    }

    public void over() {
        System.out.println("退出成功");
        System.exit(0);

    }

    /*** 查询*/
    public void inquire() {
        while (true) {
            System.out.println("请输入你要查询的类型  1-名字 2-性别 3-年龄 4 返回菜单 5-模糊查询");
            int a ;
            try {
                a = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入类型错误");
                sc.next();
                continue;
            }
            if (a == 1) {
                System.out.println("请输入你要查询的名字");
                String name ;
                try {
                    name = sc.next();
                }catch (InputMismatchException e){
                    System.out.println("输入类型错误");
                    sc.next();
                    continue;
                }

                int counter = 0;
                for (int i = 0; i < ary.size(); i++) {
                    Student student = (Student) ary.gat(i);
                    if (student.getName().equals(name)) {
                        counter++;
                        System.out.println(student.getName() +" "+ student.getGender() +" "+ student.getAge());
                    }
                }
                System.out.println("一共有"+ counter+"个你要找的人");
                break;
            } else if (a == 2) {
                System.out.println("请输入要查询的性别");
                String gender ;
                try {
                   gender = sc.next();
                }catch (InputMismatchException e){
                    System.out.println("输入类型错误");
                    sc.next();
                    continue;
                }

                int counter = 0;
                for (int i = 0; i < ary.size(); i++) {
                    Student student = (Student) ary.gat(i);
                    if (student.getGender().equals(gender)) {
                        counter++;
                        System.out.println(student.getName() +" "+ student.getGender() +" "+ student.getAge());
                    }

                }
                System.out.println("一共有"+ counter+"个你要找的人");
                break;

            }

            while (true) {
                if (a == 3) {
                    System.out.println("请输入要查询的年龄");
                    String age ;
                    try {
                        age = sc.next();
                    } catch (InputMismatchException e) {
                        System.out.println("输入有误重新输入");
                        sc.next();
                        continue;
                    }
                    int counter = 0;
                    for (int i = 0; i < ary.size(); i++) {
                        Student student = (Student) ary.gat(i);
                        if (student.getAge().equals(age)) {
                            counter++;
                            System.out.println(student.getName() +" "+ student.getGender() +" "+ student.getAge());
                        }
                    }
                    System.out.println("一共有"+ counter+"个你要找的人");
                    break;
                }
                if(a == 4){
                    menu();
                    break;
                }if(a == 5) {
                    boolean input = false;
                    System.out.println("请输入性氏");
                    String name = sc.next();
                    for(int i = 0 ; i < ary.size() ; i++){
                        Student student = (Student) ary.gat(i);
                        System.out.println(student.getName().matches("^name[\\u4e00-\\u9fa5]{1}$"));


                    }
                    break;


                }else {
                    System.out.println("输入有误重新输入");
                    break;
                }

            }
        }

    }


    public static void main(String[] args) {
        new StudentUI();
    }
}
